package com.asa.ripperview

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import kotlin.math.max

class RippleView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : AppCompatImageView(context, attrs) {


    private val paint  = Paint()
    private val paint1  = Paint()
    private val paint2  = Paint()
    private var isAnim = true

    private var valueAnimator1 : ValueAnimator?= null
    private var valueAnimator2 : ValueAnimator?= null

    private var maxRound = 0f
    private var minRound = 0f

    private var animationValue1 = 0f
    private var animationValue2 = 0f
    private var round1 = 0f
    private var round2 = 0f
    private var lastAnimState = isAnim


    init {
        val attrsValue = context.obtainStyledAttributes(attrs,R.styleable.RippleView)
        val defaultColor = attrsValue.getColor(R.styleable.RippleView_rippleColor,ContextCompat.getColor(context,R.color.color_FF8119))
        attrsValue.recycle()
        paint.isAntiAlias = true
        paint.color = defaultColor

        paint1.isAntiAlias = true
        paint1.color = defaultColor
        paint1.style = Paint.Style.FILL
        paint1.strokeWidth = context.resources.getDimensionPixelOffset(R.dimen.dp_3).toFloat()

        paint2.isAntiAlias = true
        paint2.color = defaultColor
        paint2.style = Paint.Style.FILL
        paint2.strokeWidth = context.resources.getDimensionPixelOffset(R.dimen.dp_3).toFloat()


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(measuredWidth,measuredWidth)


        maxRound  = measuredWidth/2f
        minRound = maxRound - 16
        initAnim()

    }

    fun setAnim(isAnim:Boolean){
        this.isAnim = isAnim
        postInvalidate()
    }


    private fun initAnim() {
        if (isAnim){

            valueAnimator1?.removeAllUpdateListeners()
            valueAnimator1 = ValueAnimator.ofFloat(0f,(maxRound- minRound))
            valueAnimator1?.addUpdateListener { valueAnimator->
                animationValue1 =  valueAnimator.animatedValue as Float

                paint1.alpha = ((255- (255 / (maxRound - minRound) * animationValue1)).toInt())
                round1 = animationValue1 + minRound
                invalidate()
            }
            valueAnimator1?.repeatCount = ValueAnimator.INFINITE
            valueAnimator1?.duration = 3000
            valueAnimator1?.start()


            valueAnimator2?.removeAllUpdateListeners()
            valueAnimator2 = ValueAnimator.ofFloat(0f,(maxRound - minRound))
            valueAnimator2?.addUpdateListener { valueAnimtor->
                animationValue2 = valueAnimtor.animatedValue as Float
                paint2.alpha = ((255- (255 / (maxRound - minRound) * animationValue2)).toInt())
                round2 = animationValue2 + minRound

            }
            valueAnimator2?.duration = 3000
            valueAnimator2?.repeatCount = ValueAnimator.INFINITE
            valueAnimator2?.startDelay = 1500
            valueAnimator2?.start()
        }
    }

    override fun onDraw(canvas: Canvas?) {
        if (isAnim){
            canvas?.drawCircle(measuredWidth/2f,measuredWidth/2f,round1,paint1)
            canvas?.drawCircle(measuredWidth/2f,measuredWidth/2f,round2,paint2)
        }

        canvas?.drawCircle(measuredWidth/2f,measuredWidth/2f,minRound,paint)
        super.onDraw(canvas)
    }


    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        isAnim = lastAnimState
        if (isAnim){
            initAnim()
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        lastAnimState = isAnim
        isAnim = false
        valueAnimator1?.removeAllUpdateListeners()
        valueAnimator2?.removeAllUpdateListeners()
    }


}